//
//  SeeOtherFeeVC.m
//  NtoccShipper
//
//  Created by mac on 2020/1/2.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "SeeOtherFeeVC.h"
#import "SeeOtherFeeCell.h"
@interface SeeOtherFeeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) SeeOtherFeeCell *seeOtherFeeCell;
//@property (nonatomic,strong) NSMutableArray *arrListData;
@property (nonatomic,strong) CJLabel *labMoney;
@property (nonatomic,assign) float sumMoney;
@property (nonatomic,strong) NSMutableArray *arrList;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@end

@implementation SeeOtherFeeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"查看其他费用")];
    [self setBackLeftButton:@""];
    
    [self.view addSubview:self.tableView];
    [self createTableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

- (void)getSumMOney{
    WS(ws);
    self.sumMoney = 0;
    [self.arrList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSDictionary *dic = obj;
        if ([dic[@"extraPayType"] intValue] == 0) {
            ws.sumMoney = ws.sumMoney + [obj[@"extraFeeAmount"] floatValue];
        }else{
            ws.sumMoney = ws.sumMoney - [obj[@"extraFeeAmount"] floatValue];
        }
    }];
    
    self.labMoney.text = CJStringWithFormat(@"%@",@(self.sumMoney).cj_money_description);
}
/// 创建tableview 等
- (void)createTableView{
    
    //    WS(ws);
    CGRect rcFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 0);
    FlexFrameView *header = [[FlexFrameView alloc] initWithFlex:@"AddOtherFeeHeaderView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;

    [header layoutIfNeeded];
    //    FlexFrameView *headerFooter = [[FlexFrameView alloc] initWithFlex:@"AddOtherFeeFooterView" Frame:rcFrame Owner:self];
    //    headerFooter.flexibleHeight = YES;

    //    [headerFooter layoutIfNeeded];
    self.tableView.tableHeaderView = header;
    //    self.tableView.tableFooterView = headerFooter;
}


- (void)initData{
    self.arrList = [[NSMutableArray alloc]init];
    self.page = 1;
}

- (void)loadData{

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-tms-api/orderExtraFee",CJBaseUrl);
    NSDictionary *dicPra = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
                             @"orderCode":self.strCode
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
        if (list.count > 0) {

            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.arrList removeAllObjects];
            }
            //           NSError * error ;
            //           NSArray *arr = [TradingRecordModel arrayOfModelsFromDictionaries:list error:&error];
            [ws.arrList addObjectsFromArray:list];
            [ws getSumMOney];
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];

            if (ws.arrList.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }

        }else{
           [ws.arrList removeAllObjects];
           [ws.tableView.mj_header endRefreshing];
           [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [ws.tableView reloadData];
       
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"SeeOtherFeeCell";
    SeeOtherFeeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SeeOtherFeeCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSDictionary *dic = self.arrList[indexPath.row];
    [cell setCellInfoWithDic:dic];

    return cell;
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    
    return _tableView;
}


@end
